//加载字典
function dictJudge(value, options, row) {
    //sex为自定义字典项
    var statusObj = vm.options.sex;
    for (var i = 0; i < statusObj.length; i++) {
        if (statusObj[i].value == value) {
            return '<span>' + statusObj[i].text + '</span>';
        }
    }
}
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'managerAppUser/appuser/list',
        datatype: "json",
        postData: {"shzt": "2"},
        colModel: [
            {label: 'id', name: 'id', index: 'id_', width: 50, key: true, hidden: true},
            {label: '用户id', name: 'userid', index: 'userid', width: 90},
            {label: '姓名', name: 'realName', index: 'real_name', width: 80},
            {
                label: '用户级别',
                name: 'level',
                index: 'level',
                width: 80,
                formatter: function (value, options, row) {
                    if (value == "1") {
                        return "<span>VIP</span>";
                    } else if (value == "2") {
                        return "<span>待审核</span>";
                    } else {
                        return "<span>" + value + "</span>";
                    }
                }
            },
            {label: '号牌类型', name: 'hplx', index: 'hplx', width: 70},
            {label: '车牌号码', name: 'cphm', index: 'cphm', width: 80},
            {label: '身份证号', name: 'sfzh', index: 'sfzh', width: 140},
            {label: '联系地址', name: 'address', index: 'address', width: 120},
            {label: '手机号码', name: 'phone', index: 'phone', width: 90},
            {
                label: '有无驾驶证',
                name: 'sfyjsz',
                index: 'sfyjsz',
                width: 80,
                formatter: function (value, options, row) {
                    if (value == "1") {
                        return "<span>有</span>";
                    } else if (value == "2") {
                        return "<span>无</span>";
                    } else {
                        return "<span>" + value + "</span>";
                    }

                }
            },
            {label: '驾驶证号', name: 'jszh', index: 'jszh', width: 140},
            {label: '档案编号', name: 'dabh', index: 'dabh', width: 120},
            {label: '号牌号码', name: 'hphm', index: 'hphm', width: 80, hidden: true},
            {label: '创建时间', name: 'createTime', index: 'create_time', width: 80, hidden: true},
            {label: '核发地 1 本地 2 本省外市 3 外省', name: 'hfd', index: 'hfd', width: 80, hidden: true},
            {label: '身份证扫描件正面', name: 'scanUrlZ', index: 'scan_url_z', width: 80, hidden: true},
            {label: '身份证扫描件反面', name: 'scanUrlF', index: 'scan_url_f', width: 80, hidden: true},
            {
                label: '审核状态',
                name: 'shzt',
                index: 'shzt',
                width: 80,
                formatter: function (value, options, row) {
                    if (value == "1") {
                        return "<span>审核通过</span>";
                    } else if (value == "2") {
                        return "<span>待审核</span>";
                    } else if (value == "3") {
                        return "<span>审核未通过</span>";
                    }
                }
            },
            {label: '审核不通过理由', name: 'opinion', index: 'opinion', width: 80, hidden: true}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });

    $(function () {
        $('.fancybox').fancybox();
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        appUser: {},
        queryCondition: {},
        showUpdate: true
    },
    methods: {
        query: function () {
            vm.reload();
        },
        audit: function () {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "审核";
            vm.showUpdate = false;
            vm.getInfo(id)
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.showUpdate = true;
            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.appUser.id == null ? "managerAppUser/appuser/save" : "managerAppUser/appuser/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.appUser),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "managerAppUser/appuser/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "managerAppUser/appuser/info/" + id, function (r) {
                vm.appUser = r.appUser;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            var postData = $("#jqGrid").jqGrid('getGridParam', 'postData');
            $.each(postData, (k, v) => {
                delete postData[k];
            });
            var params = {
                "shzt": $("#shzt").val() == "" ? undefined : $("#shzt").val(),
                "phone": vm.queryCondition.phone == "" ? undefined : vm.queryCondition.phone,
                "real_name": vm.queryCondition.real_name == "" ? undefined : vm.queryCondition.real_name,
                "sfzh": vm.queryCondition.sfzh == "" ? undefined : vm.queryCondition.sfzh,
                "cphm": vm.queryCondition.cphm == "" ? undefined : vm.queryCondition.cphm
            }
            $("#jqGrid").jqGrid('setGridParam', {
                page: page,
                postData: params
            }).trigger("reloadGrid");
        },
        showCreateModal: function () {
            $('#yjMModal').modal('show');
        },
        closeModal: function () {
            $('#yjMModal').modal('hide');
        },
        consented: function () {
            vm.appUser.shzt = '1';
            auditAppUser();
        },
        deny: function () {
            $('#yjMModal').modal('hide');
            vm.appUser.shzt = '3';
            auditAppUser();
        },
        exportAppUser: function (event) {
            var phone = $.isEmptyObject(vm.queryCondition.phone)?'':vm.queryCondition.phone;
            var real_name = $.isEmptyObject(vm.queryCondition.real_name)?'':vm.queryCondition.real_name;
            var sfzh = $.isEmptyObject(vm.queryCondition.sfzh)?'':vm.queryCondition.sfzh;
            var cphm = $.isEmptyObject(vm.queryCondition.cphm)?'':vm.queryCondition.cphm;
            var shzt = $.isEmptyObject($("#shzt").val())?'':$("#shzt").val();
            var param = "?phone=" +phone + "&real_name=" + real_name + "&sfzh=" + sfzh + "&cphm=" + cphm + "&shzt=" + shzt;
            window.location.href = baseURL + "managerAppUser/appuser/exportAppUser" + param;
        }
    }
});

function auditAppUser() {
    $.ajax({
        type: "POST",
        dataType: "json",
        data: JSON.stringify(vm.appUser),
        url: baseURL + "managerAppUser/appuser/auditAppUser",
        contentType: "application/json",
        success: function (data, status) {
            if (data.code == 0) {
                alert("操作成功！", (index) => {
                    vm.reload();
                })
            } else {
                alert(data.msg);
            }
        },
        error: function (xmlhttprequest, errormessage, thorwException) {
            alert(errormessage);
        }
    });
}